package com.plusapps.inkwang.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.plusapps.inkwang.R;
import com.plusapps.inkwang.VideoListActivity;
import com.plusapps.inkwang.model.Video;

public class VideoListAdapter extends ArrayAdapter<Video> {
	
	private int layoutResource;
	private ArrayList<Video> videoList;
	private LayoutInflater inflater;
	
	public VideoListAdapter (Context context, int layoutResource, ArrayList<Video> videoList) {
		super(context, layoutResource, videoList);
		this.layoutResource = layoutResource;
		this.videoList = videoList;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	

	/*
	 * http://stackoverflow.com/questions/6840372/which-the-smooth-and-fast-way-to-load-image-from-net-in-android-list-view
	 */
	
	public View getView (int position, View convertView, ViewGroup parent) {

		
	    VideoListViewHolder holder;
	    Video video = videoList.get(position);
		
		if (convertView == null) {
			convertView = inflater.inflate(layoutResource, null);
			
			holder = new VideoListViewHolder();
			holder.thumbnailView = (ImageView) convertView.findViewById(R.id.video_item_thumbnail);
			holder.titleView = (TextView) convertView.findViewById(R.id.video_item_title);
			holder.descView = (TextView) convertView.findViewById(R.id.video_item_desc);
			holder.statsView = (TextView) convertView.findViewById(R.id.video_item_stats);
			
	        convertView.setTag(holder);			
		} else {
			holder = (VideoListViewHolder) convertView.getTag();
		}

		try {
			holder.thumbnailView.setImageBitmap(VideoListActivity.ImageCache.read(video.getVid(), video.getThumbnailURL()));
			holder.titleView.setText(video.getTitle());
			holder.descView.setText(video.getVideoDesc());
			holder.statsView.setText(video.getVideoStats());
		} catch (Exception e) {
			Log.e(this.getClass().getName(), e.getMessage());
		}

        return convertView;

	}

	
	static class VideoListViewHolder {
	    ImageView thumbnailView;
	    TextView titleView;
	    TextView descView;
	    TextView statsView;
	}
	

}
